import { useEffect, useState } from 'react';

const useIndexes = (currentIndex: number, lastIndex: number): [number, () => void, () => void] => {
  const [prev, setPrev] = useState(currentIndex - 1);
  const [current, setCurrent] = useState(currentIndex);

  const [next, setNext] = useState(currentIndex + 1);

  const handlePrev = () => {
    if (prev >= 0) {
      setCurrent(prev);
      setNext(current);
      setPrev(prev - 1);
    }
  };

  const handleNext = () => {
    if (next <= lastIndex) {
      setCurrent(next);
      setNext(next + 1);
      setPrev(current);
    }
  };

  useEffect(() => {
    setPrev(currentIndex - 1);
    setCurrent(currentIndex);
    setNext(currentIndex + 1);
  }, [currentIndex]);

  return [current, handlePrev, handleNext];
};

export default useIndexes;
